const _ = require('lodash')

// 普通纯函数
function checkAge (min, age) {
  return age >= min
}
console.log(checkAge(18, 20))
console.log(checkAge(18, 22))

// 函数柯里化
// function _checkAge (min) {
//   return function (age) {
//     return age >= min
//   }
// }

const _checkAge = min => (age => age >= min)
const age18 = _checkAge(18)
console.log(age18(20))
console.log(age18(22))

// lodash中的curry
function getSum (a, b, c, d) {
  return a + b + c + d
}
const curried = _.curry(getSum)
// console.log(curried(1, 2, 3))
// console.log(curried(1)(2, 3))
// console.log(curried(1, 2)(3))

// const match = _.curry(function (reg, str) {
//   return str.match(reg)
// })
// const haveSpace = match(/\s+/g)
// const haveNumber = match(/\d+/g)
// const filter = _.curry(function (fn, arr) {
//   return arr.filter(fn)
// })
// const findSpace = filter(haveSpace)
// console.log(findSpace(['d_ddd', 'd dd']))

// 模拟curry
function curry (fn) {
  const fnLen = fn.length
  return function _curry (...args) {
    if (args.length < fnLen) {
      return function () {
        return _curry(...args.concat(Array.from(arguments)))
      }
    }
    return fn(...args)
  }
}
const match = curry(function (reg, str) {
  return str.match(reg)
})
const haveSpace = match(/\s+/g)
const haveNumber = match(/\d+/g)
const filter = curry(function (fn, arr) {
  return arr.filter(fn)
})
const findSpace = filter(haveSpace)
console.log(findSpace(['d_ddd', 'd dd']))
// const _curried = curry(getSum)
// console.log(_curried(1, 2, 3))
// console.log(_curried(1)(2, 3)(4))